/*
 * (C) 2006 Dirk W. Hoffmann. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "C64.h"

void 
VIC::getColor(ColorScheme scheme, int nr, uint8_t *r, uint8_t *g, uint8_t *b)
{
	uint8_t rgb[12][16][3] = {

		/* Source: http://www.geocities.com/Area51/Vault/8611/cbm/c64col.htm */
		
		/* CCS64 */
		{   
			{ 0x10, 0x10, 0x10 },
			{ 0xff, 0xff, 0xff },
			{ 0xe0, 0x40, 0x40 },
			{ 0x60, 0xff, 0xff },
			{ 0xe0, 0x60, 0xe0 }, 
			{ 0x40, 0xe0, 0x40 },
			{ 0x40, 0x40, 0xe0 },
			{ 0xff, 0xff, 0x40 },
			{ 0xe0, 0xa0, 0x40 },
			{ 0x9c, 0x74, 0x48 },
			{ 0xff, 0xa0, 0xa0 },
			{ 0x54, 0x54, 0x54 },
			{ 0x88, 0x88, 0x88 },
			{ 0xa0, 0xff, 0xa0 },
			{ 0xa0, 0xa0, 0xff },
			{ 0xc0, 0xc0, 0xc0 } 
		},
		
		/* VICE */
		{   
			{ 0x00, 0x00, 0x00 },
			{ 0xff, 0xff, 0xff },
			{ 0xbd, 0x18, 0x21 },
			{ 0x31, 0xe7, 0xc6 },
			{ 0xb5, 0x18, 0xe7 },
			{ 0x18, 0xd6, 0x18 },
			{ 0x21, 0x18, 0xad },
			{ 0xde, 0xf7, 0x08 },
			{ 0xbd, 0x42, 0x00 },
			{ 0x6b, 0x31, 0x00 },
			{ 0xff, 0x4a, 0x52 },
			{ 0x42, 0x42, 0x42 },
			{ 0x73, 0x73, 0x6b },
			{ 0x5a, 0xff, 0x5a },
			{ 0x5a, 0x52, 0xff },
			{ 0xa5, 0xa5, 0xa5 }
		},
		
		/* FRODO */
		{
			{ 0x00, 0x00, 0x00 },
			{ 0xff, 0xff, 0xff },
			{ 0xcc, 0x00, 0x00 },
			{ 0x00, 0xff, 0xcc },
			{ 0xff, 0x00, 0xff },
			{ 0x00, 0xcc, 0x00 },
			{ 0x00, 0x00, 0xcc },
			{ 0xff, 0xff, 0x00 },
			{ 0xff, 0x88, 0x00 },
			{ 0x88, 0x44, 0x00 },
			{ 0xff, 0x88, 0x88 },
			{ 0x44, 0x44, 0x44 },
			{ 0x88, 0x88, 0x88 },
			{ 0x88, 0xff, 0x88 },
			{ 0x88, 0x88, 0xff },
			{ 0xcc, 0xcc, 0xcc }
		},
		
		/* PC64 */
		{
			{ 0x21, 0x21, 0x21 },
			{ 0xff, 0xff, 0xff },
			{ 0xb5, 0x21, 0x21 },
			{ 0x73, 0xff, 0xff },
			{ 0xb5, 0x21, 0xb5 },
			{ 0x21, 0xb5, 0x21 },
			{ 0x21, 0x21, 0xb5 },
			{ 0xff, 0xff, 0x21 },
			{ 0xb5, 0x73, 0x21 },
			{ 0x94, 0x42, 0x21 },
			{ 0xff, 0x73, 0x73 },
			{ 0x73, 0x73, 0x73 },
			{ 0x94, 0x94, 0x94 },
			{ 0x73, 0xff, 0x73 },
			{ 0x73, 0x73, 0xff },
			{ 0xb5, 0xb5, 0xb5 }
		},
		
		/* C64S */
		{
			{ 0x00, 0x00, 0x00 },
			{ 0xfc, 0xfc, 0xfc },
			{ 0xa8, 0x00, 0x00 },
			{ 0x54, 0xfc, 0xfc },
			{ 0xa8, 0x00, 0xa8 },
			{ 0x00, 0xa8, 0x00 },
			{ 0x00, 0x00, 0xa8 },
			{ 0xfc, 0xfc, 0x00 },
			{ 0xa8, 0x54, 0x00 },
			{ 0x80, 0x2c, 0x00 },
			{ 0xfc, 0x54, 0x54 },
			{ 0x54, 0x54, 0x54 },
			{ 0x80, 0x80, 0x80 },
			{ 0x54, 0xfc, 0x54 },
			{ 0x54, 0x54, 0xfc },
			{ 0xa8, 0xa8, 0xa8 }
		},
		
		/* ALEC64 */
		{
			{ 0x00, 0x00, 0x00 },
			{ 0xfc, 0xfc, 0xfc },
			{ 0x9c, 0x00, 0x00 },
			{ 0x00, 0xbc, 0xbc },
			{ 0xbc, 0x00, 0xbc },
			{ 0x00, 0x9c, 0x00 },
			{ 0x00, 0x00, 0x9c },
			{ 0xfc, 0xfc, 0x00 },
			{ 0xfc, 0x58, 0x00 },
			{ 0x78, 0x38, 0x00 },
			{ 0xfc, 0x00, 0x00 },
			{ 0x3c, 0x3c, 0x3c },
			{ 0x7c, 0x7c, 0x7c },
			{ 0x00, 0xfc, 0x00 },
			{ 0x00, 0x00, 0xfc },
			{ 0xbc, 0xbc, 0xbc }
		},
		
		/* WIN64 */
		{
			{ 0x00, 0x00, 0x00 },
			{ 0xff, 0xff, 0xff },
			{ 0x68, 0x00, 0x14 },
			{ 0x00, 0xc0, 0xac },
			{ 0x94, 0x00, 0x98 },
			{ 0x5c, 0x98, 0x5e }, // exchanged with LTGREEN
			{ 0x04, 0x10, 0xb0 },
			{ 0xfc, 0xfc, 0x00 },
			{ 0xf9, 0x9a, 0x1a },
			{ 0x50, 0x20, 0x14 }, 
			{ 0xfc, 0x50, 0x80 },
			{ 0x46, 0x46, 0x46 },
			{ 0x73, 0x73, 0x73 },
			{ 0x24, 0xf0, 0x00 }, // exchanged with GREEN
			{ 0x5e, 0x70, 0xf2 },
			{ 0xac, 0xac, 0xac }
		}, 

		/* C64ALIVE */
		{
			{ 0x00, 0x00, 0x00 },
			{ 0xfc, 0xfc, 0xfc },
			{ 0xb0, 0x00, 0x00 },
			{ 0x00, 0xb8, 0xb8 },
			{ 0xa0, 0x00, 0xa0 },
			{ 0x00, 0xbc, 0x00 },
			{ 0x00, 0x00, 0xa0 },
			{ 0xf8, 0xfc, 0x50 },
			{ 0xcc, 0x64, 0x00 },
			{ 0x98, 0x4c, 0x28 },
			{ 0xf4, 0x88, 0x90 },
			{ 0x58, 0x58, 0x58 },
			{ 0x94, 0x94, 0x94 },
			{ 0x68, 0xfc, 0x80 },
			{ 0x68, 0x80, 0xf8 },
			{ 0xd8, 0xd8, 0xd8 }
		},
		
		/* GODOT */
		{
			{ 0x00, 0x00, 0x00 },
			{ 0xff, 0xff, 0xff },
			{ 0x88, 0x00, 0x00 },
			{ 0xaa, 0xff, 0xee },
			{ 0xcc, 0x44, 0xcc },
			{ 0x00, 0xcc, 0x55 },
			{ 0x00, 0x00, 0xaa },
			{ 0xee, 0xee, 0x77 },
			{ 0xdd, 0x88, 0x55 },
			{ 0x66, 0x44, 0x00 },
			{ 0xfe, 0x77, 0x77 },
			{ 0x33, 0x33, 0x33 },
			{ 0x77, 0x77, 0x77 },
			{ 0xaa, 0xff, 0x66 },
			{ 0x00, 0x88, 0xff },
			{ 0xbb, 0xbb, 0xbb }
		},
		
		/* C64SALLY */
		{
			{ 0x00, 0x00, 0x00 },
			{ 0xfc, 0xfc, 0xfc },
			{ 0xc8, 0x00, 0x00 },
			{ 0x00, 0xfc, 0xfc },
			{ 0xfc, 0x00, 0xfc },
			{ 0x00, 0xc8, 0x00 },
			{ 0x00, 0x00, 0xc8 },
			{ 0xfc, 0xfc, 0x00 },
			{ 0xfc, 0x64, 0x00 },
			{ 0xc0, 0x64, 0x00 },
			{ 0xfc, 0x64, 0x64 },
			{ 0x40, 0x40, 0x40 },
			{ 0x80, 0x80, 0x80 },
			{ 0x64, 0xfc, 0x64 },
			{ 0x64, 0x64, 0xf0 },
			{ 0xc0, 0xc0, 0xc0 }
		},
		
		/* PEPTO */
		{
			{ 0x00, 0x00, 0x00 },
			{ 0xff, 0xff, 0xff },
			{ 0x68, 0x37, 0x2b },
			{ 0x70, 0xa4, 0xb2 },
			{ 0x6f, 0x3d, 0x86 },
			{ 0x58, 0x8d, 0x43 },
			{ 0x35, 0x28, 0x79 },
			{ 0xb8, 0xc7, 0x6f },
			{ 0x6f, 0x4f, 0x25 },
			{ 0x43, 0x39, 0x00 },
			{ 0x9A, 0x67, 0x59 },
			{ 0x44, 0x44, 0x44 },
			{ 0x6c, 0x6c, 0x6c },
			{ 0x9a, 0xd2, 0x84 },
			{ 0x6c, 0x5e, 0xb5 },
			{ 0x95, 0x95, 0x95 }
		},
	
		/* GRAYSCALE */
		{
			{ 0x10, 0x10, 0x10 },
			{ 0xff, 0xff, 0xff },
			{ 0x70, 0x70, 0x70 },
			{ 0xE1, 0xE1, 0xE1 },
			{ 0x8c, 0x8c, 0x8c },
			{ 0xB9, 0xB9, 0xB9 },
			{ 0x55, 0x55, 0x55 },
			{ 0xed, 0xed, 0xed },
			{ 0xa5, 0xa5, 0xa5 },
			{ 0x78, 0x78, 0x78 },
			{ 0xb4, 0xb4, 0xb4 },
			{ 0x54, 0x54, 0x54 },
			{ 0x88, 0x88, 0x88 },
			{ 0xdc, 0xdc, 0xdc },
			{ 0xa7, 0xa7, 0xa7 },
			{ 0xc0, 0xc0, 0xc0 }
		}
	};
	
	assert(scheme >= 0 && scheme < 12);
	assert(nr >= 0 && nr < 16);
	
	*r = rgb[scheme][nr][0];
	*g = rgb[scheme][nr][1];
	*b = rgb[scheme][nr][2];
}


void 
VIC::setColor(int nr, uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha)
{
	uint8_t *color = (uint8_t *)(&colors[nr]);

	assert(nr >= 0 && nr < 16);
	
	color[0] = red;
	color[1] = green;
	color[2] = blue;
	color[3] = alpha;
}

void 
VIC::setColorScheme(ColorScheme scheme)
{
	uint8_t i, r, g, b;
	
	if (scheme == CUSTOM_PALETTE)
		return;

	for (i = 0; i < 16; i++) {
		getColor(scheme, i, &r, &g, &b);
		setColor(i, r, g, b);
	}

}			